package com.ciquan.server.service.impl;

import com.ciquan.server.bean.UserInfo;
import com.ciquan.server.dao.UserInfoMapper;
import com.ciquan.server.service.UserInfoService;
import com.ciquan.server.util.MD5Utils;
import com.ciquan.server.util.web.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by bjliwei2014 on 2015/2/27.
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo selectById(int id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public List<UserInfo> selectApplyUser() {
        return userInfoMapper.selectApplyUser();
    }

    @Override
    public int applyUser(int id) {
        return userInfoMapper.applyUser(id);
    }

    @Override
    public int applyRefuse(int id) {
        return userInfoMapper.applyRefuse(id);
    }

    @Override
    public List<String> tokens(String deviceType) {
        return userInfoMapper.tokens(deviceType);
    }

    @Override
    public List<UserInfo> selectMerchant() {
        return userInfoMapper.selectMerchant();
    }

    @Override
    public boolean hasUser(String phone) {
        return userInfoMapper.selectCount(phone) > 0;
    }

    @Override
    @Transactional
    public boolean registerUser(UserInfo userInfo, Result result) {
        if(!hasUser(userInfo.getMobile())) {
            userInfo.setPassword(MD5Utils.md5(userInfo.getPassword()).toLowerCase());
            userInfo.setType(0);
            userInfo.setIntro("");
            userInfo.setDeviceType("");
            userInfo.setName(userInfo.getMobile().substring(0, 3) + "****" + userInfo.getMobile().substring(7));
            if(userInfoMapper.insert(userInfo) > 0) {
                result.setSuccess(true);
                userInfo = userInfoMapper.selectByMobile(userInfo.getMobile());
                userInfo.setHash(getUserHash(userInfo.getUserid().toString()));
                result.setData(userInfo);
                result.setStatusmsg("注册成功");
                return true;
            }

            else {
                result.setStatusmsg("注册失败");
            }
        }

        else {
            result.setStatusmsg("手机号码已经注册");
        }
        return false;
    }

    @Override
    public boolean resetPassword(UserInfo userInfo, Result result) {
        if(hasUser(userInfo.getMobile())) {
            userInfo.setPassword(MD5Utils.md5(userInfo.getPassword()).toLowerCase());
            if(userInfoMapper.update(userInfo) > 0) {
                userInfo = userInfoMapper.selectByMobile(userInfo.getMobile());
                userInfo.setHash(getUserHash(userInfo.getUserid().toString()));
                result.setSuccess(true);
                result.setData(userInfo);
                result.setStatusmsg("修改密码成功");
                return true;
            }

            else {
                result.setStatusmsg("修改密码失败");
            }
        }

        else {
            result.setStatusmsg("不存在此账号");
        }

        return false;
    }

    @Override
    public List<String> mobiles() {
        return userInfoMapper.mobiles();
    }

    protected String getUserHash(String userid){
        String hash_key = "lkA*w42foahsdf9*Q@";
        String hash = MD5Utils.md5(userid + "_" + hash_key);
        return hash.toLowerCase();
    }
}
